// routes/api.js
const express = require("express");
const router = express.Router();
require("dotenv").config();
const config = require("./../server/db.config.js");
const User = require("./../models/user");
const authMiddleware = require("../middleware/auth");

const BASE_URL = process.env.API_HOST + ":" + process.env.API_PORT;
console.log(BASE_URL);

const {
  getBanners,
  getNotices,
  getCategorys,
  getFeatureds,
  getBestsellers,
  getProducts,
} = require("./../models/index");
const { batchAdd, getCartList } = require("./../models/cart");
const { getWechatSession, getUserInfo } = require("./../models/login");

console.log("API routes loaded");
// 推荐产品路由 - 使用async/await
router.get("/home/banner", async (req, res) => {
  try {
    // 获取连接
    await getBanners(res);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});
// 推荐产品路由 - 使用async/await
router.get("/home/notice", async (req, res) => {
  try {
    // 获取连接
    await getNotices(res);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});
// 推荐产品路由 - 使用async/await
router.get("/home/category", async (req, res) => {
  try {
    // 获取连接
    await getCategorys(res);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});
// 推荐产品路由 - 使用async/await
router.get("/home/featured", async (req, res) => {
  try {
    // 获取连接
    await getFeatureds(res);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});
// 推荐产品路由 - 使用async/await
router.get("/home/bstseller", async (req, res) => {
  try {
    // 获取连接
    await getBestsellers(res);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});
// 推荐产品路由 - 使用async/await
router.get("/home/recommend", async (req, res) => {
  const page = parseInt(req.query.page) || 1;
  const size = parseInt(req.query.size) || 15;
  const offset = (page - 1) * size;
  try {
    // 获取连接
    await getProducts(res, page, size, offset);
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      message: "连接数据库失败",
      error: connErr.message,
    });
  }
});

// 购物车路由
router.get("/cart", authMiddleware, (req, res) => {
  res.send("Hello Cart!");
});

// 推荐产品路由 - 使用async/await,token验证放在中间件authMiddleware中
router.post("/cart/add", authMiddleware, async (req, res) => {
  try {
    const { productId, quantity } = req.body;
    const user_id = req.user.id; // 从认证中间件获取

    await batchAdd(user_id, productId, quantity);
    res.status(200).json({ code: 0, success: true, message: "添加成功" });
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(400).json({
      success: false,
      code: 400,
      message: "添加购物车失败",
      error: connErr.message,
    });
  }
});
router.get("/cart/get", authMiddleware, async (req, res) => {
  console.log('获取购物车列表')
  try {
    const user_id = req.user.id;
    const cartItems = await getCartList(user_id);
    res.status(200).json({ success: true, code: 200, data: cartItems,total: cartItems.length });
  } catch (connErr) {
    console.error("数据库连接错误:", connErr);
    res.status(500).json({
      success: false,
      code: 500,
      message: "数据库操作失败",
      error: connErr.message,
    });
  }
});

module.exports = router;
